package com.jwgf.excel;

import com.jwgf.common.AppContext;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import jxl.Workbook;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

public class ExcelWriter {
    private WritableWorkbook workbook;
    private List<WritableSheet> sheets;
    private String filePath;

    public ExcelWriter(String name) {
        filePath = AppContext.getInstance().getFilesDir() + File.separator + name + ".xls";
        try {
            OutputStream os = new FileOutputStream(filePath);
            workbook = Workbook.createWorkbook(os);
            sheets = new ArrayList<>();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public WritableSheet addSheet(String sheetName) {
        WritableSheet sheet = workbook.createSheet(sheetName, sheets.size());
        sheets.add(sheet);
        return sheet;
    }

    public void close() {
        try {
            workbook.write();
            workbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (WriteException e) {
            e.printStackTrace();
        }
    }

    public String getFilePath() {
        return filePath;
    }


}
